home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
103_01
/
edit.c
< prev
next >
Wrap
Text File
|
1985-03-09
|
21KB
|
1,033 lines
/* Hello! This program is supposed to be an editor... in fact, the
editor just like mom used to make (oops...) I mean, just like the
QED editor found most anywhere, especially on UNIX
*/
#include "edit.inc"
main(argc,argv)
int argc;
char **argv;
{
char *exp_string;
int i;
init_buffer();
prompt_enabled = DEF_PROMPT;
sprintf(deflt_file,"DEFAULT.$$$");
print_line_number = DEFAULT_PRINT_LINE_NUMBER;
exp_string = "+-0123456789.$/?";
modified_file = NO;
printer_echo = NO;
push_point = 0;
user_command = NO;
user_string[0] = EOL;
printf("version 12.9999b by scott fluhrer and neal somos");
putchar('\n'); /* to force local version in */
if (argc>1)
if (argc==2)
{
read_file(argv[1]);
}
else
syntax_error();
for (;;)
{
if (user_command)
{
for (i=0; line[i] = user_string[i]; i++)
;
user_command = NO;
}
else
{
tline[0]='\0';
if (prompt_enabled)
{
if (print_line_number)
sprintf(tline,"%d>",dot);
else sprintf(tline,">");
}
get_line(line,tline);
}
parse = 0;
skip_space(&parse, line);
if (char_in_string(line[parse], exp_string))
{
number1 = eval_exp(&parse, line);
if (number1 == ERROR)
SYNTAX_ERROR;
skip_space(&parse, line);
if (line[parse] == COMMA)
{
parse++;
number2 = eval_exp(&parse, line);
if (number2 == ERROR)
SYNTAX_ERROR;
num_num = 2;
}
else
num_num = 1;
}
else
num_num = 0;
skip_space(&parse, line);
global = NO;
found_line = NO;
if (do_command())
continue;
oops: syntax_error();
}
}
/* This is the general command do-er, returning YES if everything
went Hunky-Dory
*/
BOOL do_command()
{
int l, start_search, i;
BOOL interact, forced, delete_after_store, print_control;
char file[MAX_LINE], replace[MAX_LINE+2], *h;
char *w;
char string1[MAX_LINE+1], string2[MAX_LINE+1];
switch( tolower( line[parse] ))
{
case 'p':
print_control = NO;
random_label: if (check_2_param())
return(NO);
while(l=get_next_line())
{
print(l, print_control);
dot = l;
}
break;
case 'l':
print_control = YES;
goto random_label;
case EOL:
if (check_blank())
return(NO);
while (l=get_next_line())
{
print(l, NO);
dot = l;
}
break;
case 'i':
if (check_1_param())
return(NO);
if (number1 == 0)
return(NO);
insert(number1);
break;
case 'a':
if (tolower( line[parse+1] ) == 'c')
{
parse++;
if (check_2_param())
return(NO);
while (l=get_next_line())
append_comment(l);
break;
}
if (check_1_param())
return(NO);
if (number1 > dollar)
return(NO);
insert(number1+1);
break;
case 'd':
if (check_2_param())
return(NO);
while(l=get_next_line())
delete(l);
break;
case 'c':
if (check_2_param())
return(NO);
if (global)
return(NO);
while (l=get_next_line())
delete(l);
insert(number1);
break;
case 'm':
delete_after_store = YES;
move_label: if (check_3_param())
return(NO);
stuffed_file = NO;
t = temp_buff;
while (l=get_next_line())
{
read_line(l, replace);
if (delete_after_store)
delete_line(l);
for (w=replace; *w; w++)
if (save_in_buff(*w))
return (NO);
if (save_in_buff(EOL))
return (NO);
}
if (stuffed_file)
seek(fd, 0, 0);
u = 0;
while (recall_from_buff(replace))
{
insert_line(number3, replace);
}
break;
case 't':
delete_after_store = NO;
goto move_label;
case 'h':
if (check_0_param())
return(NO);
if ((fd=open("edit.hlp", 0)) == ERROR)
return(NO);
read(fd, temp_buff, 8);
close(fd);
for (h=&temp_buff; *h!=CNTRL_Z; h++)
printf("%c",*h);
break;
case 'u':
if (line[parse+1] == '=')
{
if (check_user())
return(NO);
parse += 2;
if (line[parse] == EOL)
{
printf("%s\n", user_string);
break;
}
for (i=0; line[parse];)
user_string[i++] = line[parse++];
user_string[i] = EOL;
break;
}
if (check_0_param())
return(NO);
user_command = YES;
break;
case 'j':
if (check_1_param())
return(NO);
if (number1==0 OR number1==dollar)
return(NO);
read_line(number1, string1);
read_line(number1+1, string2);
if (strlen(string1) + strlen(string2) > MAX_LINE)
{
printf("Line too long\007\n");
return(YES);
}
strcat(string1, string2);
delete_line(number1+1);
delete_line(number1);
insert_line(number1+1, string1);
dot = number1+1;
break;
case 'n':
if (check_file(file))
return(NO);
if (push_point == MAX_FILES)
return(NO);
if (fopen(file, &iobuf[push_point]) == ERROR)
return(NO);
push_point++;
break;
case 'r':
if (check_file(file))
return(NO);
read_file(file);
break;
case 'w':
if (check_file(file))
return(NO);
write_file(file);
break;
case 'f':
if (line[parse+1] == '=')
{
parse++;
if (check_file(file))
return(NO);
strcpy(deflt_file, file);
break;
}
if (strcmp(deflt_file, "") == 0)
printf("\007??\n");
else
printf("%s\n", deflt_file);
break;
case '#':
if (check_0_param())
return(NO);
print_line_number ^= YES;
break;
case '>':
if (check_0_param())
return(NO);
prompt_enabled = NO;
break;
case '<':
if (check_0_param())
return(NO);
prompt_enabled = YES;
break;
case 'q':
if (line[parse+1] == '!')
{
parse++;
forced = YES;
}
else
forced = NO;
if (check_0_param())
return(NO);
if (modified_file AND NOT forced)
{
printf("Write??\007\n");
modified_file = NO;
break;
}
else
{
if (temp_used)
unlink(TEMP_FILE);
exit(YES);
}
case 's':
interact = NO;
search_label: if (check_search())
return(NO);
if (NOT global)
init_search();
start_search = place_search(&parse, line);
if (start_search == ERROR)
return(NO);
if (NOT get_replace(&parse, line, replace))
return(NO);
g_mode = p_mode = NO;
if (NOT get_options())
return(NO);
if (interact)
{
if (g_mode)
return(NO);
g_mode = YES;
}
while (l=get_next_line())
substitute(l, start_search, replace, interact);
break;
case 'x':
interact = YES;
goto search_label;
case 'g':
search_true = YES;
global_search: if (check_global())
return(NO);
global = YES;
init_search();
if (place_search(&parse, line) == ERROR)
return(NO);
return(do_command());
case 'v':
search_true = NO;
goto global_search;
case '=':
if (check_1_param())
return(NO);
printf("%d\n", number1);
break;
case '%':
if (check_1_param())
return(NO);
if (dollar == 0 OR number1 == 0 OR number1>dollar)
return(NO);
dot = number1;
for (i=max(1, dot-8); i<dot; i++)
print(i, NO);
HIGH_LIGHT;
print(dot, NO);
STOP_HIGH_LIGHT;
for (i=dot+1;i<=min(dot+8, dollar); i++)
print(i, NO);
break;
case ':':
if (check_1_param())
return(NO);
if (number1 > dollar OR number1 == 0)
return(NO);
dot = number1;
for (i=dot; i<=min(dot+16, dollar); i++)
print(i, NO);
break;
case '^':
if (check_1_param())
return(NO);
if (dot == 0)
return(NO);
for (;;)
{
if (kbhit())
{
getchar();
printf("!\007 %d\n", dot);
break;
}
print(dot, NO);
if (dot == dollar)
break;
dot++;
}
break;
case '&':
if (check_1_param())
return(NO);
if (number1 > dollar)
return(NO);
dot = number1;
for (i=max(dot-16, 1); i<=dot; i++)
print(i, NO);
break;
case QUOTE:
if (check_1_param())
return(NO);
if (number1>dollar)
return(NO);
for (i=min(number1+